package com.naodong.shenluntiku.mvp.model.data.manager;

import com.naodong.shenluntiku.mvp.model.bean.UserInfo;

import net.orange_box.storebox.StoreBox;

import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/5/26.
 */

public class UserInfoManager {

    private static UserInfoManager instance;
    private UserInfo userInfo;
    private UserInfo.UserInfoPerferences userInfoPerferences;

    public static UserInfoManager getInstance() {
        if (instance == null) {
            instance = new UserInfoManager();
        }
        return instance;
    }

    private UserInfoManager() {
        userInfoPerferences = StoreBox.create(UiUtils.getContext(), UserInfo.UserInfoPerferences.class);
    }


    public void setNickName(String newNickName) {
        if (userInfo != null) {
            userInfo.setNickname(newNickName);
            updateUserInfo();
        }
    }

    public void setAvatar(String newAvatar) {
        if (userInfo != null) {
            userInfo.setAvatar(newAvatar);
            updateUserInfo();
        }
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void saveUserInfo(UserInfo userInfo) {
        setUserInfo(userInfo);
        updateUserInfo();
    }


    public void updateUserInfo() {
        if (userInfo != null) {
            userInfoPerferences.saveUserInfo(userInfo);
        }
    }


    public UserInfo getUserInfo() {
        if (userInfo == null) {
            userInfo = getLocalUserInfo();
        }
        return userInfo;
    }

    private UserInfo getLocalUserInfo() {
        if (userInfoPerferences != null) {
            return userInfoPerferences.getUserInfo();
        }
        return null;
    }

    public void clearUserInfo() {

        if (userInfoPerferences != null) {
            userInfoPerferences.clear();
        }
        this.userInfo = null;

    }
}
